﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class ReconHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HyperLink link = ((HyperLink)Master.FindControl("hyprChangePwd"));
                link.Visible = true;

                //get user details from session object
                User user = ((User)Session["CurrentUserVO"]);

                //get dashboard for user if customer/remitter, it's different for system/admin users
                UserEntity uEntity = new UserEntity();
                List<DashboardData> dashboardList = uEntity.GetDashboardData(user);

                if (dashboardList != null && dashboardList.Count<DashboardData>() > 0)
                {
                    //Zujar-15/02/2014 START: populate a data table for dashboard display using fetched data
                    DataTable dashboard = new DataTable();
                    dashboard.Columns.Add("Aging", Type.GetType("System.String"));
                    dashboard.Columns[0].Caption = "Aging";
                    dashboard.Columns.Add("1000000001_Above", Type.GetType("System.Int32"));
                    dashboard.Columns[1].Caption = "Above 1,00,00,00,000";
                    dashboard.Columns.Add("500000001_1000000000", Type.GetType("System.Int32"));
                    dashboard.Columns[2].Caption = "50,00,00,001 - 1,00,00,00,000";
                    dashboard.Columns.Add("10000001_500000000", Type.GetType("System.Int32"));
                    dashboard.Columns[3].Caption = "1,00,00,001 - 50,00,00,000";
                    dashboard.Columns.Add("5000001_10000000", Type.GetType("System.Int32"));
                    dashboard.Columns[4].Caption = "50,00,001 - 1,00,00,000";
                    dashboard.Columns.Add("100001_5000000", Type.GetType("System.Int32"));
                    dashboard.Columns[5].Caption = "1,00,001 - 50,00,000";
                    dashboard.Columns.Add("0_100000", Type.GetType("System.Int32"));
                    dashboard.Columns[6].Caption = "Below 1,00,000";
                    dashboard.Columns.Add("TotalPendingInvoices", Type.GetType("System.Int32"));
                    dashboard.Columns[7].Caption = "Total Pending Invoices";
                    dashboard.Columns.Add("TotalAmount", Type.GetType("System.Double"));
                    dashboard.Columns[7].Caption = "Total Amount";

                    for (int x = 0; x < 30; x += 6)
                    {
                        //Each set of 6 elements in dashboardList corresponds to a particular aging period.
                        //So the aging period changes after every 6 elements.
                        DataRow row = dashboard.NewRow();
                        row[0] = dashboardList[x].FromDays.ToString() + "-" + dashboardList[x].ToDays.ToString() + " days";
                        dashboard.Rows.Add(row);
                    }

                    dashboard.Rows[4][0] = "Above 360 days";

                    for (int x = 0; x < 30; x++)
                    {
                        //Each set of 6 elements in dashboardList corresponds to a particular aging period.
                        //6 elements to be written to a row against an aging period.
                        //Divide x by 6 - quotient represents row index while remainder represents column index, first column mentions aging period.
                        dashboard.Rows[x / 6][x % 6 + 1] = dashboardList[x].InvoiceCount;
                    }

                    //populate Total Pending Invoice and Total Amount
                    int totalPendingInvoices = 0;
                    double totalAmount = 0.00;
                    for (int x = 0; x < 30; x++)
                    {
                        totalPendingInvoices += dashboardList[x].InvoiceCount;
                        totalAmount += dashboardList[x].TotalAmount;
                        if (x % 6 == 5)
                        {
                            dashboard.Rows[x / 6]["TotalPendingInvoices"] = totalPendingInvoices;
                            dashboard.Rows[x / 6]["TotalAmount"] = Math.Round((totalAmount / 100000), 2);
                            totalPendingInvoices = 0;
                            totalAmount = 0.00;
                        }
                    }
                    //Zujar-15/02/2014 END

                    //Bar Chart
                    BarChart.Titles.Add("Ageing of Pending Invoices");                    
                    BarChart.Series["PendingInvoiceCount"].ChartType = SeriesChartType.Column;
                    BarChart.Series["PendingInvoiceCount"]["BarLabelStyle"] = "Center";
                    BarChart.Series["PendingInvoiceCount"]["DrawingStyle"] = "Cylinder";
                    BarChart.Series["PendingInvoiceCount"].Color = Color.Blue;
                    BarChart.Series["PendingInvoiceCount"].IsValueShownAsLabel = true;
                    BarChart.Series["PendingInvoiceCount"].XValueMember = "Aging";
                    BarChart.Series["PendingInvoiceCount"].YValueMembers = "TotalPendingInvoices";
                    
                    BarChart.Series["PendingInvoiceAmount"].ChartType = SeriesChartType.Column;
                    BarChart.Series["PendingInvoiceAmount"]["PointWidth"] = "0.7";
                    BarChart.Series["PendingInvoiceAmount"]["BarLabelStyle"] = "Center";
                    BarChart.Series["PendingInvoiceAmount"]["PixelPointDepth"] = "99";
                    BarChart.Series["PendingInvoiceAmount"]["DrawingStyle"] = "Cylinder";
                    BarChart.Series["PendingInvoiceAmount"].Color = Color.Red;
                    BarChart.Series["PendingInvoiceAmount"].IsValueShownAsLabel = true;
                    BarChart.Series["PendingInvoiceAmount"].IsValueShownAsLabel = true;
                    BarChart.Series["PendingInvoiceAmount"].XValueMember = "";
                    BarChart.Series["PendingInvoiceAmount"].YValueMembers = "TotalAmount";
                    
                    BarChart.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
                    BarChart.ChartAreas[0].AxisX.IsLabelAutoFit = true;
                    BarChart.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
                    BarChart.Legends.Add(new Legend("Default") { Docking = Docking.Right });
                    BarChart.Legends[0].Alignment = StringAlignment.Center;
                    BarChart.Titles[0].ForeColor = Color.MidnightBlue;
                    BarChart.Titles[0].Font = new Font("Arial", 10.0f);
                    
                    BarChart.DataSource = dashboard;
                    BarChart.DataBind();

                    BarChartDataGrid.DataSource = dashboard;
                    BarChartDataGrid.DataBind();

                    //Line Chart
                    DataTable dt = (new UserEntity()).GetPendingInvoiceCountAndAmountForChart(user.CustomerID, user.UserID);

                    LineChart.Titles.Add("Status of Pending Invoices");
                    LineChart.Series["PendingInvoiceCount"].ChartType = SeriesChartType.Line;
                    LineChart.Series["PendingInvoiceAmount"].ChartType = SeriesChartType.Line;
                    LineChart.Series["PendingInvoiceCount"].BorderWidth = 2;
                    LineChart.Series["PendingInvoiceCount"].Color = Color.Blue;
                    LineChart.Series["PendingInvoiceAmount"].BorderWidth = 2;
                    LineChart.Series["PendingInvoiceAmount"].Color = Color.Red;

                    LineChart.Series["PendingInvoiceCount"].XValueMember = "PendingInvoice";
                    LineChart.Series["PendingInvoiceCount"].YValueMembers = "InvoiceCount";
                    LineChart.Series["PendingInvoiceAmount"].XValueMember = "";
                    LineChart.Series["PendingInvoiceAmount"].YValueMembers = "InvoiceAmount";
                    LineChart.Series["PendingInvoiceCount"].IsValueShownAsLabel = true;
                    LineChart.Series["PendingInvoiceAmount"].IsValueShownAsLabel = true;

                    LineChart.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
                    LineChart.ChartAreas[0].AxisX.IsLabelAutoFit = true;
                    LineChart.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
                    LineChart.Legends.Add(new Legend("Default") { Docking = Docking.Right });
                    LineChart.Legends[0].Alignment = StringAlignment.Center;
                    LineChart.Titles[0].ForeColor = Color.MidnightBlue;
                    LineChart.Titles[0].Font = new Font("Arial", 10.0f);
                    
                    LineChart.DataSource = dt;
                    LineChart.DataBind();

                    LineChartDataGrid.DataSource = dt;
                    LineChartDataGrid.DataBind();
                }
            }
            catch (Exception)
            {
                //Handle error
            }
        }

        protected void BarChartDataGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink agingLink = (HyperLink)e.Row.Controls[0].Controls[0];
                agingLink.NavigateUrl = agingLink.NavigateUrl.Replace(" days", "").Replace("Above ", "");
            }
        }
    }
}